package com.sf.dlb.mapper.dao;


import com.sf.dlb.mapper.entity.InvitationRecordEntity;
import com.sf.dlb.mapper.entity.InvitationUserEntity;
import com.sf.dlb.mapper.entity.UserGetDlbNumberEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 用户邀请抽成
 * Created by tanjing on 2019/3/20.
 */
@Mapper
public interface UserInvitationExtractMapper {
    /**
     * 查询2，3级用户-所邀请的用户
     * @return
     */
    @Select("select i.user_id,a.user_grade,i.user_ids from (select * from user_info a where a.user_grade =#{grade}) a INNER JOIN (select a.user_id,SUBSTRING_INDEX(group_concat(a.invitation_user_id order by a.create_date desc),',',#{limit}) as \"user_ids\" from user_invitation a group by a.user_id) i on a.user_id=i.user_id")
    List<InvitationUserEntity> findByInvitationUser(@Param("grade") String grade, @Param("limit") int limit);
    /**
     * id+时间-查询用户阅读获取的dlb
     * @return
     */
    @Select("select DATE_FORMAT(a.create_date,'%Y-%m-%d') as \"end_time\",a.user_id,sum(a.get_number) as \"get_number\" from new_recod a where a.user_id=#{userId} and DATE_FORMAT(a.end_time,'%Y-%m-%d')=#{time}")
    UserGetDlbNumberEntity findByUserGetNumber(@Param("userId") String userId, @Param("time") String time);
    /**
     * 查询用户邀请记录-user_invitation
     * @return
     */
    @Select("SELECT u.user_id AS 'useUserId',u.photo,u.name,u.user_grade AS 'grade',(SELECT r.state FROM user_real_info r WHERE r.user_id = a.invitation_user_id) AS \"real\",a.create_date FROM `user_invitation` a INNER JOIN user_info u ON a.invitation_user_id = u.user_id WHERE a.user_id =  #{userId} and a.del_flag=#{delFlag}\n")
    List<InvitationRecordEntity> findByInvitationRecord(@Param("userId") String userId, @Param("delFlag") String delFlag);

}
